package middlewareorid;

import middlewareorid.engine.ConfigAccess;
import middlewareorid.ui.AddBlogWindow;
import middlewareorid.ui.ListBlogWindow;
import middlewareorid.ui.ListCategoryWindow;
import middlewareorid.ui.PreferenceBlogWindow;
import middlewareorid.ui.WelcomePanel;
import waba.fx.*;
import waba.sys.Convert;
import waba.sys.Settings;
import waba.ui.*;

public class Start extends MainWindow {
	
	ConfigAccess cf = new ConfigAccess();
	private final int style = Convert.toInt(cf.getConfigFromCatalog(cf.APP_STYLE));
	private final String UI = cf.getConfigFromCatalog(cf.APP_STYLE);
	
	public Start() {
		super("Djokdja Blogs Tools", Settings.Flat);
		byte ui = 0x00;
		if (UI.equals("0")) ui = Settings.WinCE;
		if (UI.equals("1")) ui = Settings.Flat;
		if (UI.equals("2")) ui = Settings.PalmOS;		
		setBorderStyle(RECT_BORDER);
		Settings.setUIStyle(ui);
	}

	public void onStart() {
		String col0[] = { "File", "Preference", "-", "Exit" };
		String col1[] = { "Tools", "New Blog Item", "Blog Item List",
				"Update Category" };
		String col2[] = { "Help", "About" };
		mbar = new MenuBar(new String[][] { col0, col1, col2 });
		setMenuBar(mbar);
		if (Settings.isColor) {
			mbar.setCursorColor(new Color(192,192,192));
			mbar.setBorderStyle((byte) 0);
			mbar.setPopColors(Color.WHITE, Color.BLACK,
					null);
		}
		lSel = new Label("", 0x1d4c0);
		lSel.setRect(0x1d4c0, 0x1d4c0, 140, 0x186a0);
		wp = new WelcomePanel(getClientRect());
		add(wp, 0x1d4c0, 0x1d4c0);
		wp.setRect(getClientRect());
		wp.setBorderStyle((byte) 5);
		wp.setStyle(style);
	}

	public void onEvent(Event event) {
		switch (event.type) {
		case 499:
			String sel = null;
			if (event.target == mbar)
				switch (mbar.getSelectedMenuItem()) {
				case 1: // '\001'
					clearWindow();
					pbw = new PreferenceBlogWindow(getClientRect());
					add(pbw, 0x1d4c0, 0x1d4c0);
					pbw.setRect(getClientRect());
					pbw.setBorderStyle((byte) 5);
					pbw.setStyle(style);
					break;

				case 3: // '\003'
					exit(0);
					break;

				case 101: // 'e'
					clearWindow();
					abw = new AddBlogWindow(getClientRect());
					add(abw, 0x1d4c0, 0x1d4c0);
					abw.setRect(getClientRect());
					abw.setBorderStyle((byte) 5);
					abw.setStyle(style);
					break;

				case 102: // 'f'
					clearWindow();
					lbw = new ListBlogWindow(getClientRect());
					add(lbw, 0x1d4c0, 0x1d4c0);
					lbw.setRect(getClientRect());
					lbw.setBorderStyle((byte) 5);
					lbw.setStyle(style);
					break;

				case 103: // 'g'
					clearWindow();
					lcw = new ListCategoryWindow(getClientRect());
					add(lcw, 0x1d4c0, 0x1d4c0);
					lcw.setRect(getClientRect());
					lcw.setBorderStyle((byte) 5);
					lcw.setStyle(style);
					break;

				case 201:
					clearWindow();
					wp = new WelcomePanel(getClientRect());
					add(wp, 0x1d4c0, 0x1d4c0);
					wp.setRect(getClientRect());
					wp.setBorderStyle((byte) 5);
					wp.setStyle(style);
					break;
				}
			sel = mbar.getSelectedMenuItem() + " - " + sel;
			lSel.setText(sel == null ? "" : sel);
			break;
		}
	}

	private void clearWindow() {
		if (wp != null)
			remove(wp);
		if (pbw != null)
			remove(pbw);
		if (lbw != null)
			remove(lbw);
		if (abw != null)
			remove(abw);
		if (lcw != null)
			remove(lcw);
	}

	MenuBar mbar;

	Label lSel;

	Label lWarn;

	WelcomePanel wp;

	PreferenceBlogWindow pbw;

	ListBlogWindow lbw;

	ListCategoryWindow lcw;

	AddBlogWindow abw;
}